<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class PictureController extends BaseController
{
    protected $pictureModel;

    public function __construct()
    {
        parent::__construct();
        $this->pictureModel = new \app\api\model\PictureModel();
    }
    
    /**
     * 图库列表
     * @param string    $type
     * @param int       $pagesize
     * @param int       $page
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Picture');
                $check = $validate->scene('index')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->pictureModel->list($data,$this->userid));    
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 剩余次数
     * 
     */
    public function degree()
    {
        try {
            if($this->request->isPost()){
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,['picnum' => $this->pictureModel->degree($this->userid)]);    
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * @param string $unique_id
     */
    public function poll()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Picture');
                $check = $validate->scene('poll')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->pictureModel->poll($this->userid,$data['unique_id']));    
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 删除会话
     * @param string $chatid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Picture');
                $check = $validate->scene('delete')->check($data);
                if(!$check){
                    return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                if($this->pictureModel->delchat($this->userid,$data['chatid']) !== false){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);    
                }
                return $this->jsonJump(ApiErrCode::DELETE_PIC_CHAT_FAIL,ApiErrMsg::DELETE_PIC_CHAT_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::recordLog($this->userid,'abnormal',date('Y-m-d H:i:s'),'warning',$e->getMessage());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}